//
//  CTVConfig.m
//  Channltv
//
//  Created by Bruno Keymolen on 7/5/07.
//  Copyright 2007 channl.tv. All rights reserved.
//

#import "CTVConfig.h"


@implementation CTVConfig

+(id)GetInstance {
	static id _CTVConfigSingleton = nil;
	if (_CTVConfigSingleton == nil) {
		_CTVConfigSingleton = [self new];
		[_CTVConfigSingleton setup];
	}
    return _CTVConfigSingleton;
}



 -(void) setup{
	NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

	/**
	 * Version
	 */
	m_versionMajor = 0;
	m_versionMinor = 1;
	m_versionRevision = 4;


	//Temp
	m_tmpdir = [[NSHomeDirectory() stringByAppendingString:@"/channl.tv/"] retain];
	
	//Channl.tv server 
	//Put an alternative channl.tv url in  ~/channl.tv/server.location
	//ex: 10.100.1.95:3000 
	NSString* serverLocation = [NSString stringWithFormat:@"%@server.location", [self tmpdir]];
	if( [[NSFileManager defaultManager] fileExistsAtPath:serverLocation] ){
		NSError* err;
		NSMutableString* loc = [NSMutableString stringWithContentsOfFile:serverLocation encoding:NSASCIIStringEncoding error:&err];
		[loc replaceOccurrencesOfString:@"\n" withString:@"" options:nil range:NSMakeRange(0, [loc length])]; 
		m_CTVServer = loc;
		[m_CTVServer retain];
	}else{
		m_CTVServer = [[NSString alloc] initWithString:@"www.channl.tv"];
	}
	
	//Root URL's for channl.tv data
//	NSString* rootURL = [NSString stringWithFormat:@"http://%@/channels.xml", [self CTVServer]];
	NSString* rootURL = [NSString stringWithFormat:@"http://%@/api/channels", [self CTVServer]];

	m_dataRootURLs = [[NSMutableArray alloc] init];
	[m_dataRootURLs addObject:rootURL];
	
	
	
	m_mediaCacheDir = [[NSString alloc] initWithFormat:@"%@Media/", m_tmpdir];
	m_dataCacheDir = [[NSString alloc] initWithFormat:@"%@Data/", m_tmpdir];
	m_resourceCacheDir = [[NSString alloc] initWithFormat:@"%@Resource/", m_tmpdir];
	m_autoUpdateDir = [[NSString alloc] initWithFormat:@"%@AutoUpdate/", m_tmpdir];
	
			
	//Create the directorys (if needed)
	NSFileManager* nfs = [NSFileManager defaultManager];
	[nfs createDirectoryAtPath:m_tmpdir attributes:nil];
	[nfs createDirectoryAtPath:m_mediaCacheDir attributes:nil];
	[nfs createDirectoryAtPath:m_dataCacheDir attributes:nil];
	[nfs createDirectoryAtPath:m_resourceCacheDir attributes:nil];
	[nfs createDirectoryAtPath:m_autoUpdateDir attributes:nil];			
	
	
//If we use no extension or .mp4 => Quicktime (we see the Quicktime User Agent) is used to play, it does not demand HTTP Range's but downloads full content first 	
//	m_mediaBridgePrefix = @"http://bruno.local:2809/GETMEDIA.mp4?URL=";
	m_mediaBridgePrefix = @"http://bruno.local:2809/GETMEDIA.avi?URL=";
	
	//m_tmpdir = [[NSString alloc] initWithString:tmp];

	//Use defaults
	if([defaults objectForKey:CTVCONF_USERNAME] == nil)
		[defaults setObject:@"demo" forKey:CTVCONF_USERNAME];
	
	if([defaults objectForKey:CTVCONF_PASSWORD] == nil)
		[defaults setObject:@"channltv" forKey:CTVCONF_PASSWORD];

	if([defaults objectForKey:CTVCONF_DOWNLOADTHUMBS] == nil)
		[defaults setObject:@"Yes" forKey:CTVCONF_DOWNLOADTHUMBS];

	if([defaults objectForKey:CTVCONF_REMOVEOBSOLETECLIPS] == nil)
		[defaults setObject:@"Yes" forKey:CTVCONF_REMOVEOBSOLETECLIPS];

	if([defaults objectForKey:CTVCONF_DOWNLOADCLIPS] == nil)
		[defaults setObject:@"No" forKey:CTVCONF_DOWNLOADCLIPS];

	if([defaults objectForKey:CTVCONF_PWPROTECTUSERSELECTION] == nil)
		[defaults setObject:@"No" forKey:CTVCONF_PWPROTECTUSERSELECTION];

	if([defaults objectForKey:CTVCONF_USEMEDIABRIDGE] == nil)
		[defaults setObject:@"No" forKey:CTVCONF_USEMEDIABRIDGE];
		
	if([defaults integerForKey:CTVCONF_AUTOSYNCMIN] == 0)
		[defaults setInteger:30 forKey:CTVCONF_AUTOSYNCMIN];
		
	if([defaults objectForKey:CTVCONF_CHANNELVIEW] == nil)
		[defaults setObject:@"No" forKey:CTVCONF_CHANNELVIEW];
	
	if([defaults objectForKey:CTVCONF_PLAYINTRO] == nil)
		[defaults setObject:@"No" forKey:CTVCONF_PLAYINTRO];

		
		
	[[NSUserDefaults standardUserDefaults] synchronize];

	//Create the user and login
	m_activeUser = [[CTVUser alloc] init];
	[m_activeUser loginToChannelTVByAlias:[defaults objectForKey:CTVCONF_USERNAME] andPassword:[defaults objectForKey:CTVCONF_PASSWORD]];
	
 }


-(NSString*)mediaBridgePrefix{
	return m_mediaBridgePrefix;
}

-(NSDictionary*)publicsettings{
	return m_publicsettings;
}

-(NSString*)mediaCacheDir{
	return m_mediaCacheDir;
}

-(NSString*)dataCacheDir{
	return m_dataCacheDir;
}

-(NSString*)resourceCacheDir{
	return m_resourceCacheDir;
}

-(NSString*)autoUpdateDir{
	return m_autoUpdateDir;
}

-(NSString*) tmpdir{
	return m_tmpdir;
}

-(CTVUser*)activeUser{
	return m_activeUser;
}

-(NSString*)CTVServer{
	return m_CTVServer;
}

-(NSString*)userAgent{
	return @"Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.6; channl.tv:0.1) Gecko/20070725 Firefox/2.0.0.6";
}

-(NSArray*)dataRootURLs{
	return m_dataRootURLs;
}

-(int)versionMajor{
	return m_versionMajor;
}

-(int)versionMinor{
	return m_versionMinor;
}

-(int)versionRevision{
	return m_versionRevision;
}

-(NSString*)versionString{
	NSString* vs = [NSString stringWithFormat:@"%d.%d.%d", m_versionMajor, m_versionMinor, m_versionRevision];
	return vs;
}


@end
